package com.ffcs.cn.action.desktopcontrol.smPanelComponentTitle;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.componentBorderPanel.SmPanelComponentTitle;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionListener;

/**

 */
public class DialogSmPanelComponentTitle extends SmDialog {
	private SmPanelComponentTitle componentTitle;
	private JLabel label;
	private JCheckBox checkBox;
	private JButton button;
	private JComboBox<String> comboBox;

	private JRadioButton buttonLabel;
	private JRadioButton buttonButton;
	private JRadioButton buttonCheckBox;
	private JRadioButton buttonComboBox;

	public DialogSmPanelComponentTitle() {
		setTitle("SmPanelComponentTitle 示例");

		initComponents();
		initLayout();
		initListener();
	}

	private void initListener() {
		ActionListener actionListener = e -> {
			Object source = e.getSource();
			if (source == buttonLabel){
				setTitleComponent(label);
			}else if (source == buttonCheckBox){
				setTitleComponent(checkBox);
			}else if (source == buttonButton){
				setTitleComponent(button);
			}else if (source == buttonComboBox){
				setTitleComponent(comboBox);
			}
		};
		buttonLabel.addActionListener(actionListener);
		buttonCheckBox.addActionListener(actionListener);
		buttonButton.addActionListener(actionListener);
		buttonComboBox.addActionListener(actionListener);
	}

	private void setTitleComponent(JComponent component){
		componentTitle.setTitleComponent(component);
		componentTitle.updateUI();
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private Component getRightPanel() {
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder("控制面板"));
		panel.setLayout(new GridBagLayout());

		panel.add(new JLabel("设置显示标题 Component"), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setAnchor(GridBagConstraints.WEST));

		panel.add(buttonLabel, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setAnchor(GridBagConstraints.WEST));

		panel.add(buttonCheckBox, new GridBagConstraintsHelper(0, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setAnchor(GridBagConstraints.WEST));

		panel.add(buttonComboBox, new GridBagConstraintsHelper(0, 3, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setAnchor(GridBagConstraints.WEST));

		panel.add(buttonButton, new GridBagConstraintsHelper(0, 4, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));
		return panel;
	}

	private Component getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder("控件"));
		panel.add(componentTitle);
		return panel;
	}

	private void initComponents() {
		label = new JLabel("数据源设置");
		checkBox = new JCheckBox("数据源设置");
		button = new JButton("数据源设置");
		comboBox = new JComboBox<>();
		comboBox.addItem("选项1");
		comboBox.addItem("选项2");
		comboBox.addItem("选项3");

		buttonLabel = new JRadioButton("JLabel");
		buttonCheckBox = new JRadioButton("JCheckBox");
		buttonButton = new JRadioButton("JButton");
		buttonComboBox = new JRadioButton("JComboBox");
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(buttonLabel);
		buttonGroup.add(buttonCheckBox);
		buttonGroup.add(buttonButton);
		buttonGroup.add(buttonComboBox);
		buttonLabel.setSelected(true);

		JPanel panel = new JPanel();
		panel.add(new JCheckBox("是否只读"));
		panel.add(new JButton("属性"));
		componentTitle = new SmPanelComponentTitle(label, panel);
	}
}
